/*
PearReview - The peer review assistant.
Copyright (C) 2009  Dimitrij Pankratz, Anton Musichin
http://www.pearreview.com, info@pearreview.com

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

package com.pearreview.scribeplugin.csv;

import java.util.ArrayList;
import java.util.List;

import javax.swing.table.DefaultTableModel;

import com.pearreview.util.review.Finding;

public class FindingTableModel extends DefaultTableModel {
	
	private static final long serialVersionUID = -7991774784697845601L;
	
	private List<Finding> findings;
	private List<FindingModelColumnRenderer> rendererList = 
			new ArrayList<FindingModelColumnRenderer>();
	
	public void setFindings(List<Finding> findings) {
		this.findings = findings;
		fireTableDataChanged();
	}
	
	public Finding getFinding(int index) {
		return findings.get(index);
	}
	
	public int addColumn(FindingModelColumnRenderer renderer) {
		return addColumn(renderer, true);
	}
	
	public int addColumn(FindingModelColumnRenderer renderer, 
			boolean fireEvent) {
		rendererList.add(renderer);
		if(fireEvent)
			fireTableStructureChanged();
		
		return rendererList.indexOf(renderer);
	}
	
	public void insertColumn(FindingModelColumnRenderer renderer, int index) {
		insertColumn(renderer, index, true);
	}
	
	public void insertColumn(FindingModelColumnRenderer renderer, 
			int index, boolean fireEvent) {
		rendererList.add(index, renderer);
		if(fireEvent)
			fireTableStructureChanged();
	}
	
	public FindingModelColumnRenderer removeColumn(int column) {
		return removeColumn(column, true);
	}
	
	public FindingModelColumnRenderer removeColumn(int column, 
			boolean fireEvent) {
		FindingModelColumnRenderer renderer = rendererList.remove(column);
		if(fireEvent)
			fireTableStructureChanged();
		return renderer;
	}
	
	public void clearColumns() {
		rendererList.clear();
		fireTableStructureChanged();
	}
	
	public FindingModelColumnRenderer getColumn(int columnIndex) {
		return rendererList.get(columnIndex);
	}
	
	public void setColumnName(int column, String name) {
		rendererList.get(column).setName(name);
		fireTableStructureChanged();
	}
	
	public void setColumnRenderer(int column, 
			FindingModelColumnRenderer renderer) {
		rendererList.set(column, renderer);
		fireTableRowsUpdated(0, getRowCount() - 1);
	}
	
	@Override
	public int getColumnCount() {
		return rendererList.size();
	}

	@Override
	public String getColumnName(int columnIndex) {
		return rendererList.get(columnIndex).getColumnName();
	}

	@Override
	public int getRowCount() {
		if(findings == null)
			return 0;
		return findings.size();
	}
	
	@Override
	public Object getValueAt(int row, int column) {
		return rendererList.get(column).getValue(findings.get(row));
	}
	
	@Override
	public Class<?> getColumnClass(int columnIndex) {
		return rendererList.get(columnIndex).getValueClass();
	}
	
	@Override
	public boolean isCellEditable(int row, int column) {
		return rendererList.get(column).isEditable();
	}
	
	@Override
	public void setValueAt(Object aValue, int row, int column) {
		rendererList.get(column).setNewValue(findings.get(row), aValue);
	}
}